package com.example.demo.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.example.demo.constant.SystemConstant;
import com.example.demo.controller.bean.Result;
import com.example.demo.mapper.entity.*;
import com.example.demo.service.StudentAttributeService;
import com.example.demo.service.StudentBadgeService;
import com.example.demo.service.StudentCertificationService;
import com.example.demo.service.StudentPointsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Lucy
 * @create 2024-02-13 17:04
 */
@RequestMapping("myPage")
@Api(value = "我的页面", tags = {"我的页面接口"})
@RestController
@Slf4j
public class MyPageController {

    @Autowired
    private StudentAttributeService studentAttributeService;

    @Autowired
    private StudentCertificationService studentCertificationService;

    @Autowired
    private StudentBadgeService studentBadgeService;

    @Autowired
    private StudentPointsService studentPointsService;


    @ResponseBody
    @ApiOperation("获取我的页面头部的显示信息")
    @RequestMapping(value = "getMyPageHead", method = {RequestMethod.POST,  RequestMethod.GET} )
    public Result<Object> getMyPageHead(){
        int studentId = StpUtil.getLoginIdAsInt();
        Student currentStudent = studentCertificationService.selectStudentById(studentId);
        Map<String, Object> map = new HashMap<>();
        map.put("nickname", currentStudent.getNickname());
        String avatar = SystemConstant.aliyunOSSUrlPrefix + currentStudent.getAvatar();
        map.put("avatar", avatar);
        int badgeNumber = studentBadgeService.selectBadgeNumber(studentId);
        map.put("badgeNumber", badgeNumber);
        int points = studentPointsService.selectStudentPointsByStudentId(studentId).getPoints();
        map.put("points", points);
        return Result.ok(map);
    }

    @ResponseBody
    @ApiOperation(value = "获取我的徽章的显示信息")
    @RequestMapping(value = "getMyBadge", method = {RequestMethod.POST,  RequestMethod.GET})
    public Result<List<Map<String, Object>>> getMyBadge(){
        List<Map<String, Object>> badgeList4 = new ArrayList<>();
        int studentId = StpUtil.getLoginIdAsInt();
        List<Attribute> attributeList4 = studentAttributeService.selectAttribute4();
        for(Attribute attribute : attributeList4){
            Map<String, Object> badgeMap = new HashMap<>();
            List<Badge> badgeList3 = studentBadgeService.selectBadgeByAttributeId(attribute.getAttributeId());
            Badge juniorBadge = studentBadgeService.selectJuniorBadgeByAttributeId(attribute.getAttributeId());
            StudentBadge studentJuniorBadge = studentBadgeService.selectStudentBadge(studentId, juniorBadge.getBadgeId());
            if(studentJuniorBadge == null){
                badgeMap.put("badgeName", juniorBadge.getName());
                badgeMap.put("imageUrl", SystemConstant.aliyunOSSUrlPrefix + juniorBadge.getImageUrl());
                badgeMap.put("isOwn", 0);
                badgeList4.add(badgeMap);
                continue;
            }
            List<Badge> sortedBadges = badgeList3.stream()
                    .sorted(Comparator.comparing(Badge::getAttributeValue))
                    .collect(Collectors.toList());
            for(Badge badge : sortedBadges){
                StudentBadge studentBadge = studentBadgeService.selectStudentBadge(studentId, badge.getBadgeId());
                if(studentBadge == null){
                    badgeList4.add(badgeMap);
                    break;
                }else{
                    badgeMap.put("badgeName", badge.getName());
                    badgeMap.put("imageUrl", SystemConstant.aliyunOSSUrlPrefix + badge.getImageUrl());
                    badgeMap.put("isOwn", 1);
                }
            }
        }
        return Result.ok(badgeList4);
    }


    @ResponseBody
    @ApiOperation(value = "获取属性面板接口")
    @RequestMapping(value = "getAttributePanel", method = {RequestMethod.POST})
    public Result<List<StudentAttribute>> getAttributePanel() {
        int studentId = StpUtil.getLoginIdAsInt();
        List<StudentAttribute> studentAttributeList = studentAttributeService.getStudentAttributeList(studentId);
        return Result.ok(studentAttributeList);
    }
}
